<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="会员姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="会员姓名"/>
    </el-form-item>
    <el-form-item label="手机号" prop="mobileNum">
      <el-input v-model="dataForm.mobileNum" placeholder="会员手机号"/>
    </el-form-item>
    <el-form-item label="生日" prop="birthday">
      <el-date-picker
        v-model="dataForm.birthday"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="生日">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="" prop="def1" hidden>
      <el-input v-model="dataForm.def1" placeholder=""/>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isMobile } from '@/utils/validate'
  export default {
    data () {
      const validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        isLoading: false,
        dataForm: {
          superiorId: '',
          superiorName: '',
          name: '',
          type: '',
          mobileNum: '',
          birthday: '',
          idCard: '',
          nickName: '',
          def0: '',
          def1: ''
        },
        dataRule: {
          name: [
            { required: true, message: '会员姓名不能为空', trigger: 'blur' }
          ],
          mobileNum: [
            { required: true, message: '会员手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.isLoading = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/member/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.member.name
                this.dataForm.mobileNum = data.member.mobileNum
                this.dataForm.birthday = data.member.birthday
                this.dataForm.idCard = data.member.idCard
                this.dataForm.def0 = data.member.def0
                this.dataForm.def1 = data.member.def1
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/member/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'mobileNum': this.dataForm.mobileNum,
                'birthday': this.dataForm.birthday,
                'idCard': this.dataForm.idCard,
                'def0': this.dataForm.def0,
                'def1': this.dataForm.def1
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.isLoadingVisible()
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      }
    }
  }
</script>
